<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/22
 * Time: 4:25
 * QQ:1467572213
 */

namespace app\sr\controller;

use think\facade\Config;
use think\facade\Request;
use think\facade\Session;
use \app\index\model\Recharge as Recharge_m;
use think\Db;
use think\facade\App;

class Recharge extends Base
{
    public function index()
    {
//        $redirectUrl  = Request::get();
//        $arr = json_decode($redirectUrl['body'],true);
        $recharge_api = Recharge_m::recharge_api();
        $this->assign('recharge_api',$recharge_api);
        
        $czmoney = Db::name('system_config')->where('name','czmoney')->value('value');
        $czmoneys = explode(',',$czmoney);
        $this->assign('czmoney',$czmoneys);
        return $this->fetch();
    }
    
    
     public function trc20(){
        $money = input('money');
        $user_id = Session::get('user_id');
        $user_info = \app\ch\model\Base::get_user_info($user_id);
        $data['id'] = $user_info['id'];
        $data['money'] = $money;
        $this->assign('data',$data);
        return $this->fetch();
    }
    
    public function erc20(){
        $money = input('money');
        $user_id = Session::get('user_id');
        $user_info = \app\ch\model\Base::get_user_info($user_id);
        $data['id'] = $user_info['id'];
        $data['money'] = $money;
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function recharge()
    {
        
        
        
        $user_id = Session::get('user_id');
        $user_info = \app\index\model\Base::get_user_info($user_id);
        $money = Request::get('money');
        
        $type = Request::get('type');
        
        $rst = Db::name('system_config')->where('name','service_cz')->value('value');
      
        if($type == 1){
            $res = $this->flb_pay($money,$user_info);
           
            if($res['respCode'] == 'SUCCESS'){
                $order_info['order_id'] = $res['orderNo'];
                $order_info['p_order_id'] = $res['mchOrderNo'];
                $order_info['uid'] = $user_info['id'];
                $order_info['pid'] = $user_info['pid'];
                $order_info['username'] = $user_info['username'];
                $order_info['phone'] = $user_info['phone'];
                $order_info['money'] = $money;
                $order_info['service_charge'] = $money * $rst / 100;
                $order_info['type'] = 1;
                $order_info['status'] = 0;
                $order_info['create_time'] = time();
                $order_info['fl'] = '0.065';
                $order_info['sxf'] = $money * 0.065;
                $order_info['zfjk'] = "WorldWide Pay";
                $order_info['zflx'] = "菲律宾网关三类";
                $order_info['caozuo'] = "支付系统";
                $add_order = Db::name('fd_recharge')->insert($order_info);
                
                $this->redirect($res['payInfo']);
            }
        }
        
        if($type == 2){
            $res = $this->qeapay_pay($money,$user_info);
            // var_dump($res);die;
            if($res['respCode'] == 'SUCCESS'){
                $order_info['order_id'] = $res['orderNo'];
                $order_info['p_order_id'] = $res['mchOrderNo'];
                $order_info['uid'] = $user_info['id'];
                $order_info['pid'] = $user_info['pid'];
                $order_info['username'] = $user_info['username'];
                $order_info['phone'] = $user_info['phone'];
                $order_info['money'] = $money;
                $order_info['service_charge'] = $money * $rst / 100;
                $order_info['type'] = 1;
                $order_info['status'] = 0;
                $order_info['create_time'] = time();
                $order_info['fl'] = '0.065';
                $order_info['sxf'] = $money * 0.065;
                $order_info['zfjk'] = "qeapay";
                $order_info['zflx'] = "菲律宾网关二类";
                $order_info['caozuo'] = "支付系统";
                $add_order = Db::name('fd_recharge')->insert($order_info);
                
                $this->redirect($res['payInfo']);
            }
        }
        
        if($type == 3){
            $res = $this->show_pay($money,$user_info);
            // var_dump($res);die;
            if($res['respCode'] == 'SUCCESS'){
                $order_info['order_id'] = $res['orderNo'];
                $order_info['p_order_id'] = $res['mchOrderNo'];
                $order_info['uid'] = $user_info['id'];
                $order_info['pid'] = $user_info['pid'];
                $order_info['username'] = $user_info['username'];
                $order_info['phone'] = $user_info['phone'];
                $order_info['money'] = $money;
                $order_info['service_charge'] = $money * $rst / 100;
                $order_info['type'] = 1;
                $order_info['status'] = 0;
                $order_info['create_time'] = time();
                $order_info['fl'] = '0.065';
                $order_info['sxf'] = $money * 0.065;
                $order_info['zfjk'] = "qeapay";
                $order_info['zflx'] = "菲律宾网关二类";
                $order_info['caozuo'] = "支付系统";
                $add_order = Db::name('fd_recharge')->insert($order_info);
                
                $this->redirect($res['payInfo']);
            }
        }
        
        
        if($type == 4){
            $res = $this->payment($money,$user_info);
            // var_dump($res);die;
            if($res['respCode'] == 'SUCCESS'){
                $order_info['order_id'] = $res['orderNo'];
                $order_info['p_order_id'] = $res['mchOrderNo'];
                $order_info['uid'] = $user_info['id'];
                $order_info['pid'] = $user_info['pid'];
                $order_info['username'] = $user_info['username'];
                $order_info['phone'] = $user_info['phone'];
                $order_info['money'] = $money;
                $order_info['service_charge'] = $money * $rst / 100;
                $order_info['type'] = 1;
                $order_info['status'] = 0;
                $order_info['create_time'] = time();
                $order_info['fl'] = '0.065';
                $order_info['sxf'] = $money * 0.065;
                $order_info['zfjk'] = "qeapay";
                $order_info['zflx'] = "菲律宾网关二类";
                $order_info['caozuo'] = "支付系统";
                $add_order = Db::name('fd_recharge')->insert($order_info);
                
                $this->redirect($res['payInfo']);
            }
        }
        
        if($type == 8){
            $res = $this->wowpay2($money,$user_info);
           
            if($res['respCode'] == 'SUCCESS'){
                $order_info['order_id'] = $res['orderNo'];
                $order_info['p_order_id'] = $res['mchOrderNo'];
                $order_info['uid'] = $user_info['id'];
                $order_info['pid'] = $user_info['pid'];
                $order_info['username'] = $user_info['username'];
                $order_info['phone'] = $user_info['phone'];
                $order_info['money'] = $money;
                $order_info['service_charge'] = $money * $rst / 100;
                $order_info['type'] = 1;
                $order_info['status'] = 0;
                $order_info['create_time'] = time();
                $order_info['fl'] = '0.065';
                $order_info['sxf'] = $money * 0.065;
                $order_info['zfjk'] = "wowpay2";
                $order_info['zflx'] = "菲律宾网关三类";
                $order_info['caozuo'] = "支付系统";
                $add_order = Db::name('fd_recharge')->insert($order_info);
                
                $this->redirect($res['payInfo']);
            }
        }
        
        if($type == 9){
            $res = $this->qeapay2($money,$user_info);
           
            if($res['respCode'] == 'SUCCESS'){
                $order_info['order_id'] = $res['orderNo'];
                $order_info['p_order_id'] = $res['mchOrderNo'];
                $order_info['uid'] = $user_info['id'];
                $order_info['pid'] = $user_info['pid'];
                $order_info['username'] = $user_info['username'];
                $order_info['phone'] = $user_info['phone'];
                $order_info['money'] = $money;
                $order_info['service_charge'] = $money * $rst / 100;
                $order_info['type'] = 1;
                $order_info['status'] = 0;
                $order_info['create_time'] = time();
                $order_info['fl'] = '0.065';
                $order_info['sxf'] = $money * 0.065;
                $order_info['zfjk'] = "qeapay2";
                $order_info['zflx'] = "菲律宾网关三类";
                $order_info['caozuo'] = "支付系统";
                $add_order = Db::name('fd_recharge')->insert($order_info);
                
                $this->redirect($res['payInfo']);
            }
        }
        
        if($type == 10){
            $res = $this->lexmpay($money,$user_info);
           
            if($res['respCode'] == 'SUCCESS'){
                $order_info['order_id'] = $res['orderNo'];
                $order_info['p_order_id'] = $res['mchOrderNo'];
                $order_info['uid'] = $user_info['id'];
                $order_info['pid'] = $user_info['pid'];
                $order_info['username'] = $user_info['username'];
                $order_info['phone'] = $user_info['phone'];
                $order_info['money'] = $money;
                $order_info['service_charge'] = $money * $rst / 100;
                $order_info['type'] = 1;
                $order_info['status'] = 0;
                $order_info['create_time'] = time();
                $order_info['fl'] = '0.065';
                $order_info['sxf'] = $money * 0.065;
                $order_info['zfjk'] = "lexmpay";
                $order_info['zflx'] = "菲律宾网关三类";
                $order_info['caozuo'] = "支付系统";
                $add_order = Db::name('fd_recharge')->insert($order_info);
                
                $this->redirect($res['payInfo']);
            }
        }
    }
    
    
    public function payment($money,$user_info){
        header("content-Type: text/html; charset=UTF-8");
        
        $mch_id = "110009063";
        $merchant_key = "621c983f90c2433b9b9fcf8bfca2304a";
        // $mch_id = "102001002";
        // $merchant_key = "a3a0b75ae9964c7ab08d8b8125cb6715";
        
        $url = "https://payment.qeapay.com/pay/web";
        
        $data = array(
            // 'bank_code' => 'GCASH',
            'goods_name' => '充值',
            'mch_id' => $mch_id,
            'mch_order_no' => time().mt_rand(11111,99999),
            'notify_url' => Request::domain().'/index/notify/flb_notify',
            'order_date' => date("Y-m-d H:i:s"),
            'pay_type' => '1220',
            'page_url' => Request::domain().'/index/recharge/index',
            'trade_amount' => $money,
            'version' => '1.0'
        );
        
        $signStr = $this->asc_sort($data);
        $data['sign_type'] = 'MD5';
        $sign = $this->sign($signStr,$merchant_key);
        $data['sign'] = $sign;
        $result = $this->http_post_res($url,$data);
        
        $result = json_decode($result,true);
        return $result;
        
    }
    
    public function show_pay($money,$user_info){
        header("content-Type: text/html; charset=UTF-8");
        
        $mch_id = "970804888";
        $merchant_key = "06e9d47d75fd4510b9499d9c0ba73b49";
        // $mch_id = "102001002";
        // $merchant_key = "a3a0b75ae9964c7ab08d8b8125cb6715";
        
        $url = "https://payment.lexmpay.com/pay/web";
        
        $data = array(
            // 'bank_code' => 'GCASH',
            'goods_name' => '充值',
            'mch_id' => $mch_id,
            'mch_order_no' => time().mt_rand(11111,99999),
            'notify_url' => Request::domain().'/index/notify/flb_notify',
            'order_date' => date("Y-m-d H:i:s"),
            'pay_type' => '1220',
            'page_url' => Request::domain().'/index/recharge/index',
            'trade_amount' => $money,
            'version' => '1.0'
        );
        
        $signStr = $this->asc_sort($data);
        $data['sign_type'] = 'MD5';
        $sign = $this->sign($signStr,$merchant_key);
        $data['sign'] = $sign;
        // var_dump($data);die;
        $result = $this->http_post_res($url,$data);
        
        $result = json_decode($result,true);
        return $result;
        
    }
    
    
    public function qeapay_pay($money,$user_info){
        header("content-Type: text/html; charset=UTF-8");
        
        $mch_id = "104005778";
        $merchant_key = "d2ee57c3ecd14c1ea2c09923fac1457f";
        // $mch_id = "102001002";
        // $merchant_key = "a3a0b75ae9964c7ab08d8b8125cb6715";
        
        $url = "https://payment.qeapay.com/pay/web";
        
        $data = array(
            // 'bank_code' => 'GCASH',
            'goods_name' => '充值',
            'mch_id' => $mch_id,
            'mch_order_no' => time().mt_rand(11111,99999),
            'notify_url' => Request::domain().'/index/notify/flb_notify',
            'order_date' => date("Y-m-d H:i:s"),
            'pay_type' => '1720',
            'page_url' => Request::domain().'/index/recharge/index',
            'trade_amount' => $money,
            'version' => '1.0'
        );
        
        $signStr = $this->asc_sort($data);
        $data['sign_type'] = 'MD5';
        $sign = $this->sign($signStr,$merchant_key);
        $data['sign'] = $sign;
        $result = $this->http_post_res($url,$data);
        
        $result = json_decode($result,true);
        return $result;
        
    }
    
    
    public function flb_pay($money,$user_info){
        header("content-Type: text/html; charset=UTF-8");
        
        $mch_id = "777805222";
        $merchant_key = "XHNCVA8BFAJKVX5CAPS3IPST9Q6L5HCX";
        // $mch_id = "777000001";
        // $merchant_key = "66441809a4d5480195299c4934eeb605";
        
        $url = "https://interface.wowpayglobal.com/pay/web";
        
        $data = array(
            // 'bank_code' => 'GCASH',
            'goods_name' => '充值',
            'mch_id' => $mch_id,
            'mch_order_no' => time().mt_rand(11111,99999),
            'notify_url' => Request::domain().'/index/notify/flb_notify',
            'order_date' => date("Y-m-d H:i:s"),
            'pay_type' => '1730',
            'page_url' => Request::domain().'/index/recharge/index',
            'trade_amount' => $money,
            'version' => '1.0'
        );
        
        $signStr = $this->asc_sort($data);
        $data['sign_type'] = 'MD5';
        $sign = $this->sign($signStr,$merchant_key);
        $data['sign'] = $sign;
        $result = $this->http_post_res($url,$data);
        
        $result = json_decode($result,true);
        return $result;
        
    }
    
    function asc_sort($params = array())
    {
        if (!empty($params)) {
            $p = ksort($params);
            if ($p) {
                $str = '';
                foreach ($params as $k => $val) {
                    $str .= $k . '=' . $val . '&';
                }
                $strs = rtrim($str, '&');
                return $strs;
            }
        }
        return false;
    }
    public function sign($signSource,$key) {
        if (!empty($key)) {
             $signSource = $signSource."&key=".$key;
        }
        // echo $signSource;
        return md5($signSource);
    }
    
    public function http_post_res($url,$data)
    {
        $ch = curl_init();    
        curl_setopt($ch,CURLOPT_URL,$url); //支付请求地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $response=curl_exec($ch);
        curl_close($ch);

        return $response;
    }


    public function wowpay2($money,$user_info){
        header("content-Type: text/html; charset=UTF-8");
        
        $mch_id = \Env::get('wow.mch_id');
        $merchant_key =\Env::get('wow.merchant_key');
        $url = "https://interface.wowpayglobal.com/pay/web";
        
        $data = array(
            // 'bank_code' => 'GCASH',
            'goods_name' => '充值',
            'mch_id' => $mch_id,
            'mch_order_no' => time().mt_rand(11111,99999),
            'notify_url' => Request::domain().'/index/notify/wowpay2_notify',
            'order_date' => date("Y-m-d H:i:s"),
            'pay_type' => \Env::get('wow.pay_type'),
            'page_url' => Request::domain().'/index/recharge/index',
            'trade_amount' => $money,
            'version' => '1.0'
        );
        
        $signStr = $this->asc_sort($data);
        $data['sign_type'] = 'MD5';
        $sign = $this->sign($signStr,$merchant_key);
        $data['sign'] = $sign;
        $result = $this->http_post_res($url,$data);
        
        file_put_contents(__DIR__.'/../logs/发起代收wowpay2.log', date('Y-m-d H:i:s') . ': ' . var_export($data,true) . "\n\r", FILE_APPEND);
        
        $result = json_decode($result,true);
        file_put_contents(__DIR__.'/../logs/发起代收wowpay2.log', date('Y-m-d H:i:s') . ': ' . var_export($result,true) . "\n\r", FILE_APPEND);
        return $result;
        
    }
    
    public function qeapay2($money,$user_info){
        header("content-Type: text/html; charset=UTF-8");
        
        $mch_id = \Env::get('qe.mch_id');
        $merchant_key = \Env::get('qe.merchant_key');
        $url = "https://payment.qeapay.com/pay/web";
        
        $data = array(
            // 'bank_code' => 'GCASH',
            'goods_name' => '充值',
            'mch_id' => $mch_id,
            'mch_order_no' => time().mt_rand(11111,99999),
            'notify_url' => Request::domain().'/index/notify/qeapay2_notify',
            'order_date' => date("Y-m-d H:i:s"),
            'pay_type' => \Env::get('qe.pay_type'),
            'page_url' => Request::domain().'/index/recharge/index',
            'trade_amount' => $money,
            'version' => '1.0'
        );
        
        $signStr = $this->asc_sort($data);
        $data['sign_type'] = 'MD5';
        $sign = $this->sign($signStr,$merchant_key);
        $data['sign'] = $sign;
        $result = $this->http_post_res($url,$data);
        
        file_put_contents(__DIR__.'/../logs/发起代收qeapay2.log', date('Y-m-d H:i:s') . ': ' . var_export($data,true) . "\n\r", FILE_APPEND);
        
        $result = json_decode($result,true);
        file_put_contents(__DIR__.'/../logs/发起代收qeapay2.log', date('Y-m-d H:i:s') . ': ' . var_export($result,true) . "\n\r", FILE_APPEND);
        return $result;
        
    }
    
    public function lexmpay($money,$user_info){
        header("content-Type: text/html; charset=UTF-8");
        
        $mch_id = \Env::get('le.mch_id');
        $merchant_key = \Env::get('le.merchant_key');
        $url = "https://payment.lexmpay.com/pay/web";
        
        $data = array(
            // 'bank_code' => 'GCASH',
            'goods_name' => '充值',
            'mch_id' => $mch_id,
            'mch_order_no' => time().mt_rand(11111,99999),
            'notify_url' => Request::domain().'/index/notify/lexmpay_notify',
            'order_date' => date("Y-m-d H:i:s"),
            'pay_type' => \Env::get('le.pay_type'),
            'page_url' => Request::domain().'/index/recharge/index',
            'trade_amount' => $money,
            'version' => '1.0'
        );
        
        $signStr = $this->asc_sort($data);
        $data['sign_type'] = 'MD5';
        $sign = $this->sign($signStr,$merchant_key);
        $data['sign'] = $sign;
        $result = $this->http_post_res($url,$data);
        
        file_put_contents(__DIR__.'/../logs/发起代收lexmpay.log', date('Y-m-d H:i:s') . ': ' . var_export($data,true) . "\n\r", FILE_APPEND);
        
        $result = json_decode($result,true);
        file_put_contents(__DIR__.'/../logs/发起代收lexmpay.log', date('Y-m-d H:i:s') . ': ' . var_export($result,true) . "\n\r", FILE_APPEND);
        return $result;
        
    }


}